<?php

namespace App\Admin\Controllers;

use App\Area;
use App\Http\Controllers\Controller;
use App\Product;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class ProductsController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('设备管理')
            ->description('设备列表')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('设备管理')
            ->description('设备详情')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('设备管理')
            ->description('设备编辑')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('设备管理')
            ->description('新增编辑')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Product);

        $grid->id('编号');
        $grid->name('名称');
        $grid->ie('iemi编号');
        $grid->ic('sim卡编号');
        $grid->area()->name('区域名称');
        $grid->lat('维度');
        $grid->lng('经度');
        $grid->column('智能排风系统')->display(function () {
            return '<span class="label label-success"><i class="fa fa-check-circle"></i> 运行</span>';
        });
        $grid->column('空调系统')->display(function () {
            return '<span class="label label-danger"><i class="fa fa-times-circle"></i> 关闭</span>';
        });
        // $grid->status('');
        // $grid->sort('Sort');
        // $grid->created_at('Created at');
        // $grid->updated_at('Updated at');
        $grid->filter(function ($filter) {

            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            // 在这里添加字段过滤器
            $filter->equal('area_id', '区域名称')->select(Area::pluck('name', 'id'));
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Product::findOrFail($id));

        $show->id('Id');
        $show->name('Name');
        $show->ie('Ie');
        $show->ic('Ic');
        $show->area_id('Area id');
        $show->lat('Lat');
        $show->lng('Lng');
        $show->status('Status');
        $show->sort('Sort');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Product);

        $form->text('name', 'Name');
        $form->text('ie', 'Ie');
        $form->text('ic', 'Ic');
        $form->number('area_id', 'Area id');
        $form->decimal('lat', 'Lat');
        $form->decimal('lng', 'Lng');
        $form->number('status', 'Status')->default(1);
        $form->number('sort', 'Sort');

        return $form;
    }
}
